﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using QSHC.LibrarySL.MVVM;
using System.Collections.Generic;
using QSHC.QSchedule.Web.Models;

namespace QSHC.QSchedule.Silverlight.Models
{
    public class PreOrderMapping : ModelBase
    {
        Dictionary<string, int> _preOrders = new Dictionary<string, int>
        {
            { "Type_FollowUp", 1 },         // นัดติดตามการรักษา (Follow up)
            { "Type_Admission", 2 },        // นัดนอนโรงพยาบาล (Admission)
            { "Inv_Lab", 3 },               // นัดเจาะเลือด (Blood for examination)
            { "Inv_EKG", 4 },               // ตรวจคลื่นไฟฟ้าหัวใจ (Electrocardiography)
            { "Inv_Xray", 5 },              // เอ็กซเรย์ (X-ray)
            { "Inv_Dobu", 6 },              // Dobutamine stress echocardiography
            { "Inv_TTE", 7 },               // Transthoracic echocardiography (TTE)
            { "Inv_TEE", 8 },               // Transesophygeal echocardiography (TEE)
            { "Inv_Postop", 9 },            // Postoperative echocardiography
            { "Inv_EchoAdmission", 10 },    // Before admission echocardiography
            { "Inv_EST", 11 },              // ทดสอบสมรรถภาพหัวใจ: เดินสายพาน (Exercise stress test)
            { "Inv_Catheterization", 12 },  // นัดมาฉีดสี (Cardiac catheterization)
            { "Inv_Holter24", 13 },         // ติดเครื่องตรวจติดตามการทำงานหัวใจ 24 ชั่วโมง (24 hour Holter monitoring)
            { "Inv_Holter48", 14 },         // ติดเครื่องตรวจติดตามการทำงานหัวใจ 48 ชั่วโมง (48 hour Holter monitoring)
            { "Inv_PCI", 15 },              // ขยายหลอดเลือดด้วยบอลลูน (PCI)
            { "Type_RecieveDrug", 16 },     // นัดรับยา
            { "Inv_PBMV", 17 },             // ขยายลิ้นหัวใจด้วยบอลลูน (PBMV)
            { "Inv_RF", 18 },               // จี้ไฟฟ้า (RF/EP Abration)
            { "Inv_AVF", 19 },              // ทำเส้นฟอกไต (AVF)
            { "Inv_Pacemaker", 20 },        // ใส่เครื่องกระต้นหัวใจ (pacemaker/AICD )
            { "Inv_CheckPacemaker", 21 },   // นัดมาเช็คเครื่องกระตุ้นหัวใจ (Check pacemaker)
            { "Inv_Conference", 22 },       // ประชุมปรึกษาเพื่องจองคิวผ่าตัด (Cardiac Conference)
            { "Inv_ABPM", 23 },             // ABPM
            { "Ins_Instruction1", 24 },     // ไม่งดอาหารและน้ำ
            { "Ins_Instruction2", 25 },     // งดน้ำงดอาหารหลัง 2 ทุ่ม
            { "Ins_Instruction3", 26 },     // งดน้ำงดอาหารหลังเที่ยงคืน
            { "Ins_Instruction4", 27 },     // กรุณายืนใบนัดเพื่องลงทะเบียนที่ห้องบัตร ช่อง 18 และ 19
            { "Ins_Instruction5", 28 },     // ผู้ถือบัตรทองกรุณาขอใบส่งตัวมาใหม่
            { "Ins_Instruction6", 29 },     // กรุณานำยาเดิมผลการตรวจต่างๆ (ถ้ามี) มาด้วย
            { "Inv_TTT", 30 },              // Tilt Table Test
            { "Inv_Echo", 31 },             // Echo S/P
            { "Type_Operate", 32 },         // นัดติดตามหลังผ่าตัด ()
        };

        #region AppointmentTypes

        #region Property Type_FollowUp - นัดติดตามการรักษา (Follow up)
        private bool _type_FollwUp;
        public bool Type_FollowUp
        {
            get { return _type_FollwUp; }
            set
            {
                if (_type_FollwUp != value)
                {
                    _type_FollwUp = value;
                    OnPropertyChanged(() => Type_FollowUp);

                    // Validation
                    if (Type_FollowUp)
                    {
                        Type_Admission = false;
                        Type_RecieveDrug = false;
                        Type_Operate = false;
                    }
                }
            }
        }
        #endregion

        #region Property Type_Admission - นัดนอนโรงพยาบาล (Admission)
        private bool _type_Admission;
        public bool Type_Admission
        {
            get { return _type_Admission; }
            set
            {
                if (_type_Admission != value)
                {
                    _type_Admission = value;
                    OnPropertyChanged(() => Type_Admission);

                    // Validation
                    if (Type_Admission)
                    {
                        Type_FollowUp = false;
                        Type_RecieveDrug = false;
                        Type_Operate = false;
                    }
                }
            }
        }
        #endregion

        #region Property Type_RecieveDrug - นัดรับยา
        private bool _type_RecieveDrug;
        public bool Type_RecieveDrug
        {
            get { return _type_RecieveDrug; }
            set
            {
                if (_type_RecieveDrug != value)
                {
                    _type_RecieveDrug = value;
                    OnPropertyChanged(() => Type_RecieveDrug);

                    // Validation
                    if (Type_RecieveDrug)
                    {
                        Type_FollowUp = false;
                        Type_Admission = false;
                        Type_Operate = false;
                    }
                }
            }
        }
        #endregion

        #region Property Type_RecieveDrug - นัดติดตามหลังผ่าตัด
        private bool _type_Operate;
        public bool Type_Operate
        {
            get { return _type_Operate; }
            set
            {
                if (_type_Operate != value)
                {
                    _type_Operate = value;
                    OnPropertyChanged(() => Type_Operate);

                    // Validation
                    if (Type_Operate)
                    {
                        Type_FollowUp = false;
                        Type_Admission = false;
                        Type_RecieveDrug = false;
                    }
                }
            }
        }
        #endregion


        #endregion

        #region Investigations

        #region Property Inv_Lab - นัดเจาะเลือด (Blood for examination)
        private bool _inv_Lab;
        public bool Inv_Lab
        {
            get { return _inv_Lab; }
            set
            {
                if (_inv_Lab != value)
                {
                    _inv_Lab = value;
                    OnPropertyChanged(() => Inv_Lab);
                }
            }
        }
        #endregion

        #region Property Inv_EKG - ตรวจคลื่นไฟฟ้าหัวใจ (Electrocardiography)
        private bool _inv_EKG;
        public bool Inv_EKG
        {
            get { return _inv_EKG; }
            set
            {
                if (_inv_EKG != value)
                {
                    _inv_EKG = value;
                    OnPropertyChanged(() => Inv_EKG);
                }
            }
        }
        #endregion

        #region Property Inv_Xray - เอ็กซเรย์ (X-ray)
        private bool _inv_Xray;
        public bool Inv_Xray
        {
            get { return _inv_Xray; }
            set
            {
                if (_inv_Xray != value)
                {
                    _inv_Xray = value;
                    OnPropertyChanged(() => Inv_Xray);
                }
            }
        }

        #endregion

        #region Property Inv_Dobu - Dobutamine stress echocardiography
        private bool _inv_Dobu;
        public bool Inv_Dobu
        {
            get { return _inv_Dobu; }
            set
            {
                if (_inv_Dobu != value)
                {
                    _inv_Dobu = value;
                    OnPropertyChanged(() => Inv_Dobu);
                }
            }
        }
        #endregion

        #region Property Inv_TTE - Transthoracic echocardiography
        private bool _inv_TTE;
        public bool Inv_TTE
        {
            get { return _inv_TTE; }
            set
            {
                if (_inv_TTE != value)
                {
                    _inv_TTE = value;
                    OnPropertyChanged(() => Inv_TTE);
                }
            }
        }

        #endregion

        #region Property Inv_TEE - Transesophygeal echocardiography
        private bool _inv_TEE;
        public bool Inv_TEE
        {
            get { return _inv_TEE; }
            set
            {
                if (_inv_TEE != value)
                {
                    _inv_TEE = value;
                    OnPropertyChanged(() => Inv_TEE);
                }
            }
        }
            
        #endregion

        #region Property Inv_Postop - Postoperative echocardiography
        private bool _inv_Postop;
        public bool Inv_Postop
        {
            get { return _inv_Postop; }
            set
            {
                if (_inv_Postop != value)
                {
                    _inv_Postop = value;
                    OnPropertyChanged(() => Inv_Postop);
                }
            }
        }

        #endregion

        #region Property Inv_EchoAdmission - Before admission echocardiography
        private bool _inv_EchoAdmission;
        public bool Inv_EchoAdmission
        {
            get { return _inv_EchoAdmission; }
            set
            {
                if (_inv_EchoAdmission != value)
                {
                    _inv_EchoAdmission = value;
                    OnPropertyChanged(() => Inv_EchoAdmission);
                }
            }
        }
        
        #endregion

        #region Property Inv_EST - ทดสอบสมรรถภาพหัวใจ: เดินสายพาน (Exercise stress test)
        private bool _inv_EST;
        public bool Inv_EST
        {
            get { return _inv_EST; }
            set
            {
                if (_inv_EST != value)
                {
                    _inv_EST = value;
                    OnPropertyChanged(() => Inv_EST);
                }
            }
        }

        #endregion

        #region Property Inv_Catheterization - นัดมาฉีดสี (Cardiac catheterization)
        private bool _inv_Catheterization;
        public bool Inv_Catheterization
        {
            get { return _inv_Catheterization; }
            set
            {
                if (_inv_Catheterization != value)
                {
                    _inv_Catheterization = value;
                    OnPropertyChanged(() => Inv_Catheterization);
                }
            }
        }

        #endregion

        #region Property Inv_Holter24 - ติดเครื่องตรวจติดตามการทำงานหัวใจ 24 ชั่วโมง (24 hour Holter monitoring)
        private bool _inv_Holter24;
        public bool Inv_Holter24
        {
            get { return _inv_Holter24; }
            set
            {
                if (_inv_Holter24 != value)
                {
                    _inv_Holter24 = value;
                    OnPropertyChanged(() => Inv_Holter24);
                }
            }
        }

        #endregion

        #region Property Inv_Holter48 - ติดเครื่องตรวจติดตามการทำงานหัวใจ 48 ชั่วโมง (48 hour Holter monitoring)
        private bool _inv_Holter48;
        public bool Inv_Holter48
        {
            get { return _inv_Holter48; }
            set
            {
                if (_inv_Holter48 != value)
                {
                    _inv_Holter48 = value;
                    OnPropertyChanged(() => Inv_Holter48);
                }
            }
        }

        #endregion

        #region Property Inv_PCI - ขยายหลอดเลือดด้วยบอลลูน (PCI)
        private bool _inv_PCI;
        public bool Inv_PCI
        {
            get { return _inv_PCI; }
            set
            {
                if (_inv_PCI != value)
                {
                    _inv_PCI = value;
                    OnPropertyChanged(() => Inv_PCI);
                }
            }
        }

        #endregion

        #region Property Inv_PBMV - ขยายลิ้นหัวใจด้วยบอลลูน (PBMV)
        private bool _inv_PBMV;
        public bool Inv_PBMV
        {
            get { return _inv_PBMV; }
            set
            {
                if (_inv_PBMV != value)
                {
                    _inv_PBMV = value;
                    OnPropertyChanged(() => Inv_PBMV);
                }
            }
        }

        #endregion

        #region Property Inv_RF - จี้ไฟฟ้า (RF/EP Abration)
        private bool _inv_RF;
        public bool Inv_RF
        {
            get { return _inv_RF; }
            set
            {
                if (_inv_RF != value)
                {
                    _inv_RF = value;
                    OnPropertyChanged(() => Inv_RF);
                }
            }
        }

        #endregion

        #region Property Inv_AVF - ทำเส้นฟอกไต (AVF)
        private bool _inv_AVF;
        public bool Inv_AVF
        {
            get { return _inv_AVF; }
            set
            {
                if (_inv_AVF != value)
                {
                    _inv_AVF = value;
                    OnPropertyChanged(() => Inv_AVF);
                }
            }
        }

        #endregion

        #region Property Inv_Pacemaker - ใส่เครื่องกระต้นหัวใจ (pacemaker/AICD )
        private bool _inv_Pacemaker;
        public bool Inv_Pacemaker
        {
            get { return _inv_Pacemaker; }
            set
            {
                if (_inv_Pacemaker != value)
                {
                    _inv_Pacemaker = value;
                    OnPropertyChanged(() => Inv_Pacemaker);
                }
            }
        }

        #endregion

        #region Property Inv_CheckPacemaker - นัดมาเช็คเครื่องกระตุ้นหัวใจ (Check pacemaker)
        private bool _inv_CheckPacemaker;
        public bool Inv_CheckPacemaker
        {
            get { return _inv_CheckPacemaker; }
            set
            {
                if (_inv_CheckPacemaker != value)
                {
                    _inv_CheckPacemaker = value;
                    OnPropertyChanged(() => Inv_CheckPacemaker);
                }
            }
        }

        #endregion

        #region Property Inv_Conference - ประชุมปรึกษาเพื่องจองคิวผ่าตัด (Cardiac Conference)
        private bool _inv_Conference;
        public bool Inv_Conference
        {
            get { return _inv_Conference; }
            set
            {
                if (_inv_Conference != value)
                {
                    _inv_Conference = value;
                    OnPropertyChanged(() => Inv_Conference);
                }
            }
        }

        #endregion

        #region Property Inv_ABPM - ABPM
        private bool _inv_ABPM;
        public bool Inv_ABPM
        {
            get { return _inv_ABPM; }
            set
            {
                if (_inv_ABPM != value)
                {
                    _inv_ABPM = value;
                    OnPropertyChanged(() => Inv_ABPM);
                }
            }
        }

        #endregion

        #region Property Inv_TTT - TTT
        private bool _inv_TTT;
        public bool Inv_TTT
        {
            get { return _inv_TTT; }
            set
            {
                if (_inv_TTT != value)
                {
                    _inv_TTT = value;
                    OnPropertyChanged(() => Inv_TTT);
                }
            }
        }

        #endregion

        #region Property Inv_Echo - Echo S/P
        private bool _inv_Echo;
        public bool Inv_Echo
        {
            get { return _inv_Echo; }
            set
            {
                if (_inv_Echo != value)
                {
                    _inv_Echo = value;
                    OnPropertyChanged(() => Inv_Echo);
                }
            }
        }

        #endregion

        #endregion

        #region Instruction

        #region Property Ins_Instruction1 - ไม่งดอาหารและน้ำ
        private bool _ins_Instruction1;
        public bool Ins_Instruction1
        {
            get { return _ins_Instruction1; }
            set
            {
                if (_ins_Instruction1 != value)
                {
                    _ins_Instruction1 = value;
                    OnPropertyChanged(() => Ins_Instruction1);
                }
            }
        }

        #endregion

        #region Property Ins_Instruction2 - งดน้ำงดอาหารหลัง 2 ทุ่ม
        private bool _ins_Instruction2;
        public bool Ins_Instruction2
        {
            get { return _ins_Instruction2; }
            set
            {
                if (_ins_Instruction2 != value)
                {
                    _ins_Instruction2 = value;
                    OnPropertyChanged(() => Ins_Instruction2);
                }
            }
        }
        
        #endregion

        #region Property Ins_Instruction3 - งดน้ำงดอาหารหลังเที่ยงคืน
        private bool _ins_Instruction3;
        public bool Ins_Instruction3
        {
            get { return _ins_Instruction3; }
            set
            {
                if (_ins_Instruction3 != value)
                {
                    _ins_Instruction3 = value;
                    OnPropertyChanged(() => Ins_Instruction3);
                }
            }
        }

        #endregion

        #region Property Ins_Instruction4 - กรุณายืนใบนัดเพื่องลงทะเบียนที่ห้องบัตร ช่อง 18 และ 19
        private bool _ins_Instruction4;
        public bool Ins_Instruction4
        {
            get { return _ins_Instruction4; }
            set
            {
                if (_ins_Instruction4 != value)
                {
                    _ins_Instruction4 = value;
                    OnPropertyChanged(() => Ins_Instruction4);
                }
            }
        }

        #endregion

        #region Property Ins_Instruction5 - ผู้ถือบัตรทองกรุณาขอใบส่งตัวมาใหม่
        private bool _ins_Instruction5;
        public bool Ins_Instruction5
        {
            get { return _ins_Instruction5; }
            set
            {
                if (_ins_Instruction5 != value)
                {
                    _ins_Instruction5 = value;
                    OnPropertyChanged(() => Ins_Instruction5);
                }
            }
        }

        #endregion

        #region Property Ins_Instruction6 - กรุณานำยาเดิมผลการตรวจต่างๆ (ถ้ามี) มาด้วย
        private bool _ins_Instruction6;
        public bool Ins_Instruction6
        {
            get { return _ins_Instruction6; }
            set
            {
                if (_ins_Instruction6 != value)
                {
                    _ins_Instruction6 = value;
                    OnPropertyChanged(() => Ins_Instruction6);
                }
            }
        }

        #endregion

        #endregion

        public PreOrderMapping()
        {

        }

        //public PreOrderMapping(IEnumerable<PreOrder> preorders)
        //{
        //    #region AppointmentTypes

        //    Type_FollowUp = preorders.SingleOrDefault(p => p.PreOrderID == 1) != null;
        //    Type_Admission = preorders.SingleOrDefault(p => p.PreOrderID == 2) != null;
        //    Type_RecieveDrug = preorders.SingleOrDefault(p => p.PreOrderID == 18) != null;

        //    #endregion

        //    #region Investigations

        //    Inv_Lab = preorders.SingleOrDefault(p => p.PreOrderID == 3) != null;
        //    Inv_EKG = preorders.SingleOrDefault(p => p.PreOrderID == 4) != null;

        //    #endregion
        //}

        public PreOrderMapping(IEnumerable<PreOrder> preorders)
        {
            foreach (var preOrder in _preOrders)
            {
                var isChecked = preorders.SingleOrDefault(p => p.PreOrderID == preOrder.Value) != null;

                this.GetType().GetProperty(preOrder.Key).SetValue(this, isChecked, null);
            }
        }

        public IEnumerable<int> GetPreOrders()
        {
            var preOrdersIntList = new List<int>();

            foreach (var preOrder in _preOrders)
            {
                bool isChecked = (bool)this.GetType().GetProperty(preOrder.Key).GetValue(this, null);

                if (isChecked)
                    preOrdersIntList.Add(preOrder.Value);
            }

            return preOrdersIntList;
        }
    }
}
